package com.ysj.demo.accompanynetwork.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.util.SpUtil;
import com.ysj.demo.accompanynetwork.Status;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.bean.LoginResultBean;
import com.ysj.demo.accompanynetwork.bean.RestRsp;
import com.ysj.demo.accompanynetwork.mvp.presenter.LoginPresenter;
import com.ysj.demo.accompanynetwork.mvp.view.LoginView;
import com.ysj.demo.accompanynetwork.util.ToolUtil;

public class LoginActivity extends BaseActivity implements LoginView, View.OnClickListener {
    private LoginPresenter loginPresenter;
    private TextView titleTv, toRegisterTv;
    private EditText nickEt, passwordEt;
    private Button loginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        StatusBarUtil.fullScreen(this);
        initView();
        initListener();

    }

    private void initView() {
        titleTv = findViewById(R.id.top_tv);
        titleTv.setText("登录");
        nickEt = findViewById(R.id.nick_et);
        passwordEt = findViewById(R.id.password_et);
        toRegisterTv = findViewById(R.id.to_register_tv);
        loginBtn = findViewById(R.id.login_btn);
        loginPresenter = new LoginPresenter();
        loginPresenter.bind(this);//绑定View和Presenter
    }

    private void initListener() {
        toRegisterTv.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //去注册
            case R.id.to_register_tv:
                Intent intent = new Intent(getApplication(), RegisterActivity.class);
                startActivity(intent);
                break;
            //登录
            case R.id.login_btn:
                loginPresenter.login();
                break;
        }
    }

    @Override
    public String getAccount() {
        return nickEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return passwordEt.getText().toString().trim();
    }

    @Override
    public void loginSuccess(RestRsp<LoginResultBean> body) {
        Log.i("登录LoginActivity", new Gson().toJson(body));
        switch (body.getCode()) {
            case 0:
                ToolUtil.ToastShow(getApplication(), "登录成功");
                if (!TextUtils.isEmpty(body.getData().getSessionid())) {
                    SpUtil.setParam(getApplicationContext(), SpUtil.SESSION_ID, body.getData().getSessionid());
                    SpUtil.setParam(getApplicationContext(), SpUtil.USER_ID, body.getData().getUserid());
                }
                Intent intent = new Intent(getApplication(), MainActivity.class);
                startActivity(intent);
                Log.i("LoginActivity", body.toString());
                break;
            case 10019:
                ToolUtil.ToastShow(getApplication(), Status.SERVER_ERROR.getDesc());
                break;
            default:
                SpUtil.clearSpData(getApplication(), SpUtil.SESSION_ID);
                break;
        }
    }

    @Override
    public void showNetworkError() {
        ToolUtil.ToastShow(getApplication(), "网络异常");
    }

    @Override
    public void showVerifyFailed() {
        ToolUtil.ToastShow(getApplication(), "账号或密码错误");
    }
}
